<template>
  <div class="total-box">
    <!-- 左侧树结构 -->
    <div class="head-container">
      <el-tree
        style="height: 100%"
        :data="deptOptions"
        :props="defaultProps"
        :expand-on-click-node="false"
        ref="tree"
        highlight-current
        node-key="id"
        :default-expanded-keys="defaultExpandedKeys"
        @node-click="handleNodeClick"
      >
        <template slot-scope="{node,data}">
          <span class="el-tree-node_box" :title="data.name">{{ data.name }}</span>
        </template>
      </el-tree>
    </div>

    <!-- 右侧 -->
    <div class="app-container">
      <el-row :gutter="20" style="height: 100%">
        <!--用户数据-->
        <el-col :span="24" :xs="24">
          <HkSearchform
            :data="queryParams"
            :field-list="filterInfo.fieldList"
            :list-type-info="dictList"
            @handleSearch="handleQuery"
            @handleReset="resetQuery"
            @handleEvent="handleEvent"
          />

          <el-row :gutter="10" style="margin-left:0px;">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:dept:add']"
            >新建</el-button>
            <!-- <el-button
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
                v-hasPermi="['system:dept:remove']"
              >删除</el-button
            >-->
            <el-button icon="el-icon-download" size="mini" @click="importTemplate">下载模板</el-button>
            <el-button @click="handleImport" size="mini" v-hasPermi="['system:dept:import']">导入</el-button>
            <el-button @click="handleExport" size="mini">导出</el-button>
          </el-row>

          <hk-table
            ref="tableRef"
            :loading="loading"
            :tableData="tableData"
            :tableColumns="tableList"
            :showIndex="true"
            :total="total"
            :operate="true"
            :tableHeight="80"
            :query-params="queryParams"
            @handleSelectionChange="handleSelectionChange"
            @changePage="getListData"
          >
            <template #orgType="scope">
              <dict-label :options="dictList.org_typeList" :value="scope.row.orgType" />
            </template>
            <template #unitCategory="scope">
              <dict-label :options="dictList.unitTypeList" :value="scope.row.unitCategory" />
            </template>
            <template #secQualifications="scope">
              <dict-label
                :options="dictList.orgQualificationList"
                :value="scope.row.secQualifications"
              />
            </template>
            <template #operate="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:dept:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:dept:remove']"
              >删除</el-button>
            </template>
          </hk-table>
        </el-col>
      </el-row>
    </div>
    <!-- 新增和编辑的对话框 -->
    <EditDialog
      ref="editDialogRef"
      :title="title"
      @handleAddOff="getList"
      :dictList="dictList"
      :treeId="treeId"
     :total="total"
    ></EditDialog>
    <!-- 输出 -->

    <Upload ref="import_dia" @submit="getList" :url="importUrl"></Upload>
  </div>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import HkTable from '@/components/HKTable/index.vue'
import EditDialog from './component/editDialog.vue'
import HkSearchform from '@/components/HKSearchform/index.vue'
import DictLabel from '@/components/Common/dictLabel.vue'
import Upload from './component/Upload'
import { listDept, getDictsBatch, orgPage, zoneselect, listByType, treeselect, delDept } from '@/api/system/dept'

export default {
  components: {
    Treeselect,
    HkTable,
    EditDialog,
    HkSearchform,
    DictLabel,
    Upload
  },
  dicts: ['role_status', 'org_type'],
  data() {
    return {
      defaultExpandedKeys:['root'],
      treeId: '',
      // 搜索项配置
      filterInfo: {
        fieldList: [
          { label: '部门名称', type: 'input', field: 'name' },

          {
            label: '机构类型',
            type: 'select',
            field: 'orgType',
            list: 'org_typeList'
          }
        ]
      },
      importUrl: window.baseUrl+'/system/org/import ',
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      // 表格数据
      tableData: [], //  表格
      tableList: [
        { label: '部门编号', param: 'orgNo' },
        { label: '名称', param: 'orgFullName' },
        { label: '机构类型', param: 'orgType', type: 'slot' },
        { label: '单位类别', param: 'unitCategory', type: 'slot' },
        { label: '保密资质', param: 'secQualifications', type: 'slot' },
        { label: '负责人', param: 'personLiable' },
        { label: '联系电话', param: 'telephone' }
      ],
      // 弹出层标题
      title: '',
      // 部门树选项
      deptOptions: undefined,
      // 树结构参数
      defaultProps: {
        children: 'children',
        label: 'name'
      },

      // 查询参数
      queryParams: {
        size: 10,
        current: 1
      },
      // 列表规则
      dictList: {
        foList: [
          {
            label: '是',
            value: '0'
          },
          {
            label: '否',
            value: '1'
          }
        ],

        // 部门类型
        org_typeList: [],
        unitTypeList: [],
        orgQualificationList: [],
        zoneList: [],
        orgList: [],
        statusList: [
          {
            label: '正常',
            value: 0
          },
          {
            label: '禁用',
            value: -2
          }
        ]
      }
    }
  },

  mounted() {
    this.getList()
  },
  methods: {
    // 获取字典值
    getDictList() {
      getDictsBatch('org_type,unitType,orgQualification').then(res => {
        for (let key in res) {
          this.dictList[key + 'List'] = res[key].map(item => {
            return {
              value: item.dictVal,
              label: item.dictLabel
            }
          })
        }
      })

      zoneselect().then(res => {
        this.dictList.zoneList = res
      })


      treeselect().then(res => {
        this.dictList.orgList = res
      })

    },

    /** 查询用户列表 */
    getList(val) {
      this.getDictList()
      listDept().then(res => {
        this.deptOptions = this.handleTree(res, 'id')
      })
      this.getListData(val)
    },
    getListData(val) {
      this.loading = true
      let queryParam = { ...this.queryParams, ...val }
      orgPage(queryParam).then(res => {
        this.total = res.total
        this.tableData = res.list
        this.loading = false
      })
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.current = 1
      this.queryParams.size = 10
      this.treeId = data.id
      this.getListData({ id: data.id })
    },

    /** 搜索按钮操作 */
    handleQuery(val) {
      this.queryParams.current = 1
      this.getListData()
    },
    // 重置表单
    resetQuery() {
      for (const key in this.queryParams) {
        this.queryParams[key] = undefined
      }
      this.queryParams.current = 1
      this.queryParams.size = 10
      this.getListData()
    },
    // 搜索框变化
    handleEvent(val) {
      let field = val.field
      let value = val.value
      this.queryParams = {
        ...this.queryParams,
        ...{
          [field]: value
        }
      }
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.title = '添加'
      this.$refs.editDialogRef.show()
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.title = '编辑'
      this.$refs.editDialogRef.show(row.id)
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids
      this.$modal
        .confirm('是否确认删除所选数据项？')
        .then(function() {
          return delDept(id)
        })
        .then(() => {
          this.$modal.msgSuccess('删除成功')
          this.getList()
        })
        .catch(error => {
          console.log(error)
        })
    },

    // 修改状态
    handleStatusChange(row) {},
    /** 导出按钮操作 */
    handleExport() {
      this.gDownload(
        window.baseUrl+'/system/org/export',
        {
          ...this.queryParams
        },
        `组织机构信息.xlsx`
      )
    },
    /** 导入按钮操作 */
    handleImport() {
      this.$refs.import_dia.upload.open = true
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download(window.baseUrl+'/system/org/importTemplate', {}, `组织机构导入模版.xlsx`)
    }
  }
}
</script>
<style lang="scss" scoped>
.total-box {
  font-size: 14px;
  display: flex;
  width: 100%;
  height: 100%;
}
.head-container {
  overflow: auto;
  padding: 20px;
  height: 100%;
  width: 270px;
  border-right: 1px solid rgba(0, 0, 0, 0.08);
}
.app-container {
  flex: 1;
  width: calc(100% - 340px);
}
.el-col {
  height: 100%;
  display: flex;
  flex-direction: column;
  &::v-deep .targetTable {
    flex: 1;
    overflow: auto;
  }
}
::v-deep .el-row {
  display: flex;
  flex-wrap: wrap;
}
</style>
